package vnc.server.rfb;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.awt.image.Raster;
import java.io.IOException;

import com.sun.java.swing.plaf.windows.resources.windows;

import vnc.diagnostics.Stopwatch;


/**
*
* @author mar
*/
public class ScreenHandler {
   
   private final Robot robot;
   
   private final int desktopWidth;
   private final int desktopHeight;
   
   private final int viewportWidth;
   private final int viewportHeight;
   
   
   private int[] rgbData;
   
   public ScreenHandler(int width, int height) throws AWTException{
       this.robot = new Robot();
       this.viewportWidth = width;
       this.viewportHeight = height;
       this.rgbData = new int[width * height];
       
       Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
       desktopWidth = (int) screenDimension.getWidth();
       desktopHeight = (int) screenDimension.getHeight();
       
   }
   
   /*
    *
    * @param viewportX
    * @param viewportY
    * @param viewportWidth
    * @param viewportHeight
    * @throws java.io.IOException
    * @return
    */
   public int[] createScreenCapture(int viewportX, int viewportY) throws IOException {
       /* creates the screenshot image */
       Image image = robot.createScreenCapture(new Rectangle(viewportX, viewportY, viewportWidth, viewportHeight));
       Raster ras = ((BufferedImage) image).getData();
       DataBufferInt db = (DataBufferInt) ras.getDataBuffer();
       return db.getData();
   }
}
